import os

from utils.AdminPermissionUtils import adminPermissionUtils
from utils.hosts_crawler import hostsCrawler
from utils.hosts_util import hostsUtils


def hostupdate(ip_addr,sys_hosts_file=os.path.join(os.environ["SystemRoot"],"System32","drivers","etc","hosts")):
    """
    更新hosts文件内容，获取GitHub最新ip地址，读取系统hosts文件内容并将其GitHub相关dns对应ip地址成最新ip地址
    :param ip_addr:GitHub最新ip地址
    :param sys_hosts_file:系统hosts文件路径
    :return:
    """
    hostsutils=hostsUtils(sys_hosts_file)
    dns_list=["github.com",
              "gist.github.com",
              "assets-cdn.github.com",
              "raw.githubusercontent.com",
              "gist.githubusercontent.com",
              "cloud.githubusercontent.com",
              "camo.githubusercontent.com",
              "avatars0.githubusercontent.com",
              "avatars1.githubusercontent.com",
              "avatars2.githubusercontent.com",
              "avatars3.githubusercontent.com",
              "avatars4.githubusercontent.com",
              "avatars5.githubusercontent.com",
              "avatars6.githubusercontent.com",
              "avatars7.githubusercontent.com",
              "avatars8.githubusercontent.com"]

    ip_list=[ip_addr]*len(dns_list)

    hostsutils.ip_to_dns_update(ip_list,dns_list)



def main():
    # craw_url="https://tool.chinaz.com/dns?type=a&host=github.com&ip="
    craw_url="https://ip.tool.chinaz.com/github.com"
    hostsdict=hostsCrawler.url_crawler(craw_url)
    sys_hosts_file=os.path.join(os.environ["SystemRoot"],"System32","drivers","etc","hosts")
    hostupdate(hostsdict["ip"],sys_hosts_file)
if __name__ == '__main__':
    try:
        main()
    except PermissionError as e:
        print(e)
        print("尝试以管理员权限执行……")
        adminPermissionUtils.AdminExecutor(__file__)
        print("执行完成！")